"""

书写公共的工具函数
"""
import functools

from flask import current_app
from flask import g
from flask import session

from info.models import User


def do_index_class(num):   # num 接收　｜  之前的loop.index
    if num == 1:
        return "first"
    elif num == 2:
        return "second"
    elif num == 3:
        return "third"
    else:
        return ""


def user_login_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        # 获取用户登录信息
        user_id = session.get("user_id")
        # 判断用户是否登录
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)  # 获取到登录的这个用户对象
            except Exception as e:
                current_app.logger.error(e)

        g.user = user

        return view_func(*args, **kwargs)
    return wrapper